package com.hethongviet.preventingmachine.helps;

import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.models.SMS;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
/**Handle SMS received*/
@SuppressWarnings("deprecation")
public class SmsReceiverDoor extends BroadcastReceiver
{
	private static OnSMSMessageListener onSMSMessageListener = null;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		/* get the SMS message passed in */
		Bundle bundle = intent.getExtras();

		SmsMessage[] msgs = null;

		if (bundle != null && onSMSMessageListener != null)
		{
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++)
			{
				boolean isRightMessage = false;

				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

				if (msgs[i] == null)
					continue;

				String message = msgs[i].getMessageBody().toString();
				String originNumber = msgs[i].getOriginatingAddress();

				if (message == null || message.equals(""))
					continue;
				if (originNumber == null || originNumber.equals(""))
					continue;

				Utils.debug("receive message: " + message);
				Utils.debug("From number: " + originNumber);

				String rootNum = SMSHelperDoor.getDeviceNumber();

				if (rootNum != null && !rootNum.equals(""))
				{
					try
					{
						Utils.debug(" rootnum = "
								+ rootNum.subSequence(rootNum.length() - 9,
										rootNum.length()));

						isRightMessage = originNumber.contains(rootNum
								.subSequence(rootNum.length() - 9,
										rootNum.length()));
					} catch (StringIndexOutOfBoundsException e)
					{
						e.printStackTrace();
					}
				}

				if (!isRightMessage)
				{
					Utils.debug(" Message synchronize!!!!!1111");
					isRightMessage = message
									.contains(SMSHelperDoor.CODE_SYNCHRONIZE_NUMBERS_CONTROL);
				}

				if (isRightMessage)
				{
					Utils.debug(" Get message to process! and remove from inbox");

					SMS sms = new SMS();
					sms.setMessage(message);
					sms.setRecipient(originNumber);

					onSMSMessageListener.onSMSMessageIncoming(sms);

					/* Cancel message, abort saving in Inbox folder */
					abortBroadcast();
					SMSHelperDoor.cancelTimeout();
				}
			}

		}

	}

	public void deleteALLSMS(Context context)
	{
		Uri uri = Uri.parse("content://sms");
		ContentResolver contentResolver = context.getContentResolver();

		Cursor cursor = contentResolver.query(uri, null, null, null, null);

		while (cursor.moveToNext())
		{
			long thread_id = cursor.getLong(1);
			Uri threadUri = Uri.parse("content://sms/conversations/"
					+ thread_id);
			contentResolver.delete(threadUri, null, null);
		}

	}

	public static void setOnSMSMessageListener(
			OnSMSMessageListener SMSMessageListener)
	{
		onSMSMessageListener = SMSMessageListener;
	}
}
